﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;

namespace Xceed.Silverlight.Data
{
  internal class SortPosition : SourcePosition
  {
    internal SortPosition(
      IList<SortDescription> sorting,
      IList<object> sortValues,
      StartPosition startingPosition )
    {
      if( sorting == null )
        throw new ArgumentNullException( "sorting" );

      if( sortValues == null )
        throw new ArgumentNullException( "sortValues" );

      if( sorting.Count < sortValues.Count )
        throw new InvalidOperationException( "The specified sortValues are not consistent with sorting." );

      m_sorting = new ReadOnlyCollection<SortDescription>( sorting );
      m_sortValues = new ReadOnlyCollection<object>( sortValues );
      m_startingPosition = startingPosition;
    }


    #region StartPosition Property

    internal StartPosition StartPosition
    {
      get
      {
        return m_startingPosition;
      }
    }

    private StartPosition m_startingPosition;

    #endregion StartPosition Property

    #region SortingReference Property

    internal IList<SortDescription> SortingReference
    {
      get
      {
        return m_sorting;
      }
    }

    private ReadOnlyCollection<SortDescription> m_sorting; //null

    #endregion SortingReference Property

    #region SortValues Property

    internal IList<object> SortValues
    {
      get
      {
        return m_sortValues;
      }
    }

    private ReadOnlyCollection<object> m_sortValues; //null

    #endregion SortValues Property

#if DEBUG
    public override string ToString()
    {
      System.Text.StringBuilder sb = new System.Text.StringBuilder( "SortPosition(" );
      for( int i=0;i < m_sortValues.Count;i++ )
      {
        if(i > 0)
        {
          sb.Append(", ");
        }
        object value = m_sortValues[ i ];
        sb.Append( value != null ? value.ToString() : "(null)" );
      }
      sb.Append( ")" );
      return sb.ToString();
    }
#endif  //DEBUG

    internal static SortPosition Create<T>(
      DataSourceContext context,
      T dataItem,
      StartPosition startPosition,
      IContextualItemHelper<T> dataItemHelper ) where T : RawDataItem
    {
      if( context == null )
        throw new ArgumentNullException( "context" );

      if( dataItem == null )
        throw new ArgumentNullException( "dataItem" );

      if( dataItemHelper == null )
        throw new ArgumentNullException( "dataItemHelper" );

      List<SortDescription> sortOrder = context.GetCurrentCompleteSortOrder().ToList();
      List<object> sortValues = ( from sort in sortOrder
                                  select dataItemHelper.GetMemberValue( context, sort.PropertyName, dataItem ) ).ToList();

      return new SortPosition( sortOrder, sortValues, startPosition );
    }
  }
}
